<template>
    <el-dialog :title="form.activityId ? '编辑' : '新增'" v-model="visible"
      :close-on-click-modal="false" draggable>
      <el-form ref="dataFormRef" :model="form" :rules="dataRules" formDialogRef label-width="90px" v-loading="loading">
       <el-row :gutter="24">
    <el-col :span="12" class="mb20">
      <el-form-item label=" 活动名称" prop="activityName">
        <el-input v-model="form.activityName" placeholder="请输入 活动名称"/>
      </el-form-item>
      </el-col>

    <el-col :span="12" class="mb20">
      <el-form-item label=" 活动标题" prop="title">
        <el-input v-model="form.title" placeholder="请输入 活动标题"/>
      </el-form-item>
      </el-col>

         <el-col :span="24" class="mb20">
           <el-form-item prop="picture" label="活动图片">
             <ImageUpload
                 v-model:imageUrl="form.picture"
                 :file-url="true"
                 :action="uploadAction"
                 :headers="uploadHeaders"
             >
               <template #empty>
                 <el-icon><Promotion /></el-icon>
                 <span>请上传图片</span>
               </template>
             </ImageUpload>
           </el-form-item>
         </el-col>
      <el-row>
        <el-form-item label="活动时间" prop="time">
            <el-date-picker
                v-model="form.time"
                type="datetimerange"
                start-placeholder="开始时间"
                end-placeholder="结束时间"
                format="YYYY-MM-DD HH:mm:ss"
                value-format="YYYY-MM-DD HH:mm:ss"
                date-format="YYYY-MM-DD"
                time-format="HH:mm:ss">
            </el-date-picker>
        </el-form-item>
      </el-row>

      <el-col :span="12" class="mb20">
           <el-form-item label="活动状态" prop="status">
             <el-radio-group v-model="form.status">
               <el-radio v-for="(item, index) in status"
                         :key="index"
                         :value="item.value"
                         :label="item.value">{{ item.label }}</el-radio>
             </el-radio-group>
           </el-form-item>
         </el-col>
    <el-col :span="12" class="mb20">
       <el-form-item label=" 活动内容" prop="content">
          <el-input v-model="form.content" placeholder="请输入 活动内容"/>
        </el-form-item>
       </el-col>

			</el-row>
      </el-form>
      <template #footer>
        <span class="dialog-footer">
          <el-button @click="visible = false">取消</el-button>
          <el-button type="primary" @click="onSubmit" :disabled="loading">确认</el-button>
        </span>
      </template>
    </el-dialog>
</template>

<script setup lang="ts" name="CarActivityDialog">
import {useDict} from "/@/hooks/dict";
import { useMessage } from "/@/hooks/message";
import { getObj, addObj, putObj } from '/@/api/hu/carActivity'
import { UploadFilled } from '@element-plus/icons-vue'
const emit = defineEmits(['refresh']);
const ImageUpload = defineAsyncComponent(() => import('/@/components/Upload/Image.vue'));

// 定义变量内容
const dataFormRef = ref();
const visible = ref(false)
const loading = ref(false)
// 定义字典
const { status } = useDict('status')
// 提交表单数据
const form = reactive({
		activityId:'',
	  activityName: '',
	  status: null,
    time:'',
    start:'',
    end:'',
	  title: '',
	  picture: '',
	  content: '',
});

// 定义校验规则
const dataRules = ref({
        activityName: [{required: true, message: '不能为空', trigger: 'blur'}],
        status: [{required: true, message: '不能为空', trigger: 'blur'}],
        title: [{required: true, message: '不能为空', trigger: 'blur'}],
        time: [{required: true, message: '不能为空', trigger: 'blur'}],
        picture: [{required: true, message: '不能为空', trigger: 'blur'}],
        content: [{required: true, message: '不能为空', trigger: 'blur'}],
})

// 打开弹窗
const openDialog = (id: string) => {
  visible.value = true
  form.activityId = ''

  // 重置表单数据
	nextTick(() => {
		dataFormRef.value?.resetFields();
	});

  // 获取carActivity信息
  if (id) {
    form.activityId = id
    getcarActivityData(id)
  }
};

// 提交
const onSubmit = async () => {
	const valid = await dataFormRef.value.validate().catch(() => {});
	if (!valid) return false;
  form.start = form.time[0]
  form.end = form.time[1]
  console.log(form.end)
	try {
    loading.value = true;
		form.activityId ? await putObj(form) : await addObj(form);
		useMessage().success(form.activityId ? '修改成功' : '添加成功');
		visible.value = false;
		emit('refresh');
	} catch (err: any) {
		useMessage().error(err.msg);
	} finally {
    loading.value = false;
    form.time = ''
  }
};


// 初始化表单数据
const getcarActivityData = (id: string) => {
  // 获取数据
  loading.value = true
  getObj(id).then((res: any) => {
    Object.assign(form, res.data)
  }).finally(() => {
    loading.value = false
  })
};
// 上传配置
const uploadAction = import.meta.env.VITE_ADMIN_PROXY_PATH + '/file/upload';
const uploadHeaders = {
  'Authorization': 'Bearer ' + localStorage.getItem('token') // 根据实际认证方式调整
};

// 暴露变量
defineExpose({
  openDialog
});
</script>

